﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BugNET.UserInterfaceLayer;
using BugNET.BLL;
using BugNET.Common;
using BugNET.Entities;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI.DataVisualization.Charting;

namespace BugNET.DashBoardPages
{
    public partial class Default : BasePage
    {
        Project CurrentProject;
        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (!UserManager.HasPermission(Convert.ToInt32(Request.Params["pid"]), Common.Permission.ViewDashboard.ToString()))
                Response.Redirect("~/Errors/AccessDenied.aspx");

            Page.Title = GetLocalResourceObject("PageTitle").ToString();
            ProjectId = Request.QueryString.Get("pid", 0);

            if (ProjectId == 0)
                ErrorRedirector.TransferToSomethingMissingPage(Page);

            CurrentProject = ProjectManager.GetById(ProjectId);

            if (CurrentProject == null)
            {
                ErrorRedirector.TransferToNotFoundPage(Page);
                return;
            } 
 
            if (!IsPostBack)
            {
                BindOptions();
            }

           var isSuccess= GenerateGraph();
           if (!isSuccess)
               return;

            if (this.ChartTypeList.SelectedItem.ToString() == "Pie")
                ProjectChart.Series["Default"].ChartType = SeriesChartType.Pie;

            else
                ProjectChart.Series["Default"].ChartType = SeriesChartType.Doughnut;

            if (this.ShowLegend.Checked)
                ProjectChart.Series["Default"]["PieLabelStyle"] = "Disabled";

            else
                ProjectChart.Series["Default"]["PieLabelStyle"] = "Inside";

            ProjectChart.Series["Default"].Label = "#VALX" + "\n" + " (#VALY)(#PERCENT)";
            ProjectChart.Series["Default"].LegendText = "#VALX" + "(#VALY)";

            UserChart.Series["Default"].Label = "#VALX" + "\n" + " (#VALY)(#PERCENT)";
            UserChart.Series["Default"].LegendText = "#VALX" +"(#VALY)";

            // Set chart type and title
            ProjectChart.Series["Default"].ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), this.ChartTypeList.SelectedItem.ToString(), true);


            // Set labels style
            ProjectChart.Series["Default"]["PieLabelStyle"] = this.LabelStyleList.SelectedItem.ToString();

            // Set Doughnut hole size
            ProjectChart.Series["Default"]["DoughnutRadius"] = this.HoleSizeList.SelectedItem.ToString();

            // Disable Doughnut hole size control for Pie chart
            this.HoleSizeList.Enabled = (this.ChartTypeList.SelectedItem.ToString() != "Pie");

            ProjectChart.Series["Default"].Points[1]["Exploded"] = ShowExploded.Checked.ToString();

            // Enable 3D
            ProjectChart.ChartAreas["ChartArea"].Area3DStyle.Enable3D = CheckboxShow3D.Checked;

            ProjectChart.Series[0]["PieDrawingStyle"] = this.Dropdownlist1.SelectedItem.ToString();

            // Pie drawing style
            if (this.CheckboxShow3D.Checked)
                this.Dropdownlist1.Enabled = false;

            else
                this.Dropdownlist1.Enabled = true;

            if (this.ShowLegend.Checked)
                this.ProjectChart.Legends[0].Enabled = true;
            else
                this.ProjectChart.Legends[0].Enabled = false;



            if (this.ChartTypeList.SelectedItem.ToString() == "Pie")
                UserChart.Series["Default"].ChartType = SeriesChartType.Pie;

            else
                UserChart.Series["Default"].ChartType = SeriesChartType.Doughnut;

            if (this.ShowLegend.Checked)
                UserChart.Series["Default"]["PieLabelStyle"] = "Disabled";

            else
                UserChart.Series["Default"]["PieLabelStyle"] = "Inside";

            // Set chart type and title
            UserChart.Series["Default"].ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), this.ChartTypeList.SelectedItem.ToString(), true);
            UserChart.Titles[0].Text = DropAssignedTo.SelectedText.ToString() + " Issue Status";

            // Set labels style
            UserChart.Series["Default"]["PieLabelStyle"] = this.LabelStyleList.SelectedItem.ToString();

            // Set Doughnut hole size
            UserChart.Series["Default"]["DoughnutRadius"] = this.HoleSizeList.SelectedItem.ToString();

            // Disable Doughnut hole size control for Pie chart
            this.HoleSizeList.Enabled = (this.ChartTypeList.SelectedItem.ToString() != "Pie");

            // Explode selected country

            UserChart.Series["Default"].Points[1]["Exploded"] = ShowExploded.Checked.ToString();


            // Enable 3D
            UserChart.ChartAreas["ChartArea"].Area3DStyle.Enable3D = CheckboxShow3D.Checked;

            UserChart.Series[0]["PieDrawingStyle"] = this.Dropdownlist1.SelectedItem.ToString();


            if (this.ShowLegend.Checked)
                this.UserChart.Legends[0].Enabled = true;
            else
                this.UserChart.Legends[0].Enabled = false;

        }

        public bool GenerateGraph()
        {
            int milestoneId = DropMilestone.SelectedValue;
            
            string userName = DropAssignedTo.SelectedValue;
            if (userName == null)
                return false;
            List<Entities.DashBoard> lstDashBoardData = DashBoardManager.GetDashBoard_ClosureState(ProjectId, milestoneId, userName);

            if (lstDashBoardData.Count == 0)
                return false;
            // Populate series data
            string[] xValues = { "Open", "Closed" };
            double[] yValues_project = { lstDashBoardData[0].TotalOpenIssue, lstDashBoardData[0].TotalClosedIssue };
            ProjectChart.Series["Default"].Points.DataBindXY(xValues, yValues_project);


            double[] yValues_user = { lstDashBoardData[0].UserOpenIssues, lstDashBoardData[0].UserClosedIssue };
            UserChart.Series["Default"].Points.DataBindXY(xValues, yValues_user);
            return true;
        }

        /// <summary>
        /// Binds the options.
        /// </summary>
        private void BindOptions()
        {
            //Get milestones
            DropMilestone.DataSource = MilestoneManager.GetByProjectId(ProjectId);
            DropMilestone.DataBind();
            DropMilestone.RenameDefault("-- ALL --");


            //Get Users
            DropAssignedTo.DataSource = UserManager.GetUsersByProjectId(ProjectId, true);
            DropAssignedTo.DisplayDefault = false;
            DropAssignedTo.DataBind();

        }

        /// <summary>
        /// Reset Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.Url.ToString());
        }
    }
}